package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.MedicalBaseUserScheduler;
import com.ruoyi.system.mapper.MedicalBaseUserSchedulerMapper;
import com.ruoyi.system.service.IMedicalBaseUserSchedulerService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MedicalBaseGroupScheduler;
import com.ruoyi.system.service.IMedicalBaseGroupSchedulerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 部门排班记录Controller
 *
 * @author ruoyi
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/system/groupscheduler")
public class MedicalBaseGroupSchedulerController extends BaseController {
    @Autowired
    private IMedicalBaseGroupSchedulerService medicalBaseGroupSchedulerService;
    @Autowired
    private IMedicalBaseUserSchedulerService medicalBaseUserSchedulerService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询部门排班记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedicalBaseGroupScheduler medicalBaseGroupScheduler) {
        startPage();
        List<MedicalBaseGroupScheduler> list = medicalBaseGroupSchedulerService.selectMedicalBaseGroupSchedulerList(medicalBaseGroupScheduler);
        return getDataTable(list);
    }

    /**
     * 导出部门排班记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:export')")
    @Log(title = "部门排班记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicalBaseGroupScheduler medicalBaseGroupScheduler) {
        List<MedicalBaseGroupScheduler> list = medicalBaseGroupSchedulerService.selectMedicalBaseGroupSchedulerList(medicalBaseGroupScheduler);
        ExcelUtil<MedicalBaseGroupScheduler> util = new ExcelUtil<MedicalBaseGroupScheduler>(MedicalBaseGroupScheduler.class);
        util.exportExcel(response, list, "部门排班记录数据");
    }

    /**
     * 获取部门排班记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(medicalBaseGroupSchedulerService.selectMedicalBaseGroupSchedulerById(id));
    }

    /**
     * 新增部门排班记录
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:add')")
    @Log(title = "部门排班记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicalBaseGroupScheduler medicalBaseGroupScheduler) {
        long time = new Date().getTime();

        medicalBaseGroupScheduler.setId(time + "");
        List<MedicalBaseUserScheduler> medicalBaseUserScheduler = medicalBaseUserSchedulerService.selectMedicalBaseUserSchedulerList(null);
        List<SysUser> sysUsers = userService.selectUserList(new SysUser());
        for (MedicalBaseUserScheduler m:medicalBaseUserScheduler) {
            SysUser sysUser = userService.selectUserById(Long.parseLong(m.getUserid() + ""));
            m.setSysUser(sysUser);
            System.out.println(sysUser);
        }

        List<String> list = new ArrayList<String>();
            for (SysUser u:sysUsers) {
                if(u.getUserId()>100)  list.add(u.getUserName());

            }
       String a=list.toString();
            //星期一
        medicalBaseGroupScheduler.setMondaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setMondayafternoon(a.substring(1,a.length()-1));
        //星期二
        medicalBaseGroupScheduler.setTuesdaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setTuesdayafternoon(a.substring(1,a.length()-1));
        //星期三
        medicalBaseGroupScheduler.setWednesdaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setWednesdayafternoon(a.substring(1,a.length()-1));
        //星期四
        medicalBaseGroupScheduler.setThursdaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setThursdayafternoon(a.substring(1,a.length()-1));
        //星期五
        medicalBaseGroupScheduler.setFridaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setFridayafternoon(a.substring(1,a.length()-1));
        //星期六
        medicalBaseGroupScheduler.setSaturdaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setSaturdayafternoon(a.substring(1,a.length()-1));
        //星期天
        medicalBaseGroupScheduler.setSundaymorning(a.substring(1,a.length()-1));
        medicalBaseGroupScheduler.setSundayafternoon(a.substring(1,a.length()-1));
        for (MedicalBaseUserScheduler m:medicalBaseUserScheduler) {
            if(m.getRestdate()==1&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setMondaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==1&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setMondayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==2&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setTuesdaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==2&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setTuesdayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==3&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setWednesdaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==3&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setWednesdayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==4&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setThursdaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==4&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setThursdayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==5&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setFridaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==5&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setFridayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==6&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setSaturdaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==6&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setSaturdayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==7&&m.getDatedetail()==0){
                medicalBaseGroupScheduler.setSundaymorning(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
            if(m.getRestdate()==7&&m.getDatedetail()==1){
                medicalBaseGroupScheduler.setSundayafternoon(a.substring(1,a.length()-1).replace(m.getSysUser().getUserName()+",",""));
            }
        }


        medicalBaseGroupScheduler.setCreateTime(new Date());
//        Map<MedicalBaseUserScheduler,SysUser> map=new HashMap<>();
//        for (MedicalBaseUserScheduler m:medicalBaseUserScheduler) {
//            for (SysUser u: sysUsers) {
//                if(u.getUserId().equals(m.getUserid())){
//                    map.put(m,u);
//                }
//            }
//        }
//        for (Map.Entry<MedicalBaseUserScheduler,SysUser> entry : map.entrySet()) {
//            MedicalBaseUserScheduler key = entry.getKey();
//            SysUser value = entry.getValue();
//
//        }
        return toAjax(medicalBaseGroupSchedulerService.insertMedicalBaseGroupScheduler(medicalBaseGroupScheduler));
    }

    /**
     * 修改部门排班记录
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:edit')")
    @Log(title = "部门排班记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicalBaseGroupScheduler medicalBaseGroupScheduler) {
        return toAjax(medicalBaseGroupSchedulerService.updateMedicalBaseGroupScheduler(medicalBaseGroupScheduler));
    }

    /**
     * 删除部门排班记录
     */
    @PreAuthorize("@ss.hasPermi('system:groupscheduler:remove')")
    @Log(title = "部门排班记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(medicalBaseGroupSchedulerService.deleteMedicalBaseGroupSchedulerByIds(ids));
    }
}
